package com.yh.homestay.service.impl;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.homestay.dao.CapitalItemLikeMapper;
import com.yh.homestay.dao.CapitalItemMapper;
import com.yh.homestay.pojo.CapitalItem;
import com.yh.homestay.pojo.CapitalItemExample;
import com.yh.homestay.service.CapitalItemService;
import com.yh.homestay.common.HomeStayResult;
import com.yh.homestay.common.ItemEditResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品管理
 */
@Service
public class CapitalItemServiceImpl implements CapitalItemService {
    private Log logger = LogFactory.getLog(CapitalItemServiceImpl.class);

    @Autowired
    private CapitalItemMapper capitalItemMapper;

    @Autowired
    private CapitalItemLikeMapper capitalItemFindNameMapper;

    /**
     * 展示商品
     * @return
     */
    @Override
    public HomeStayResult findAll(Integer page, Integer limit, String itemName) {

        Map map = new HashMap();
        map.put("itemName",itemName);
        PageHelper.startPage(page,limit);
        List<CapitalItem> capitalItems = capitalItemFindNameMapper.selectByItemName(map);
        PageInfo info = new PageInfo(capitalItems);
        return new HomeStayResult(0,"ok",(int)info.getTotal(),info.getList());
    }


    /**
     * 添加商品
     * @paramcapitalItem
     */
    @Override
    public HomeStayResult addCapitalItem(CapitalItem capitalItem) {
        try {
            Integer item = capitalItemMapper.insert(capitalItem);
            return HomeStayResult.ok();
        }catch (Exception e){
            logger.debug(e);
            return HomeStayResult.build(500,"添加出问题了,请稍后再试");
        }
    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @Override
    public ItemEditResult findItemById(Integer id) {
        CapitalItemExample example = new CapitalItemExample();
        CapitalItemExample.Criteria criteria = example.createCriteria();
        criteria.andItemIdEqualTo(id);
        List<CapitalItem> list = capitalItemMapper.selectByExample(example);
        return new ItemEditResult(200,"ok",list.get(0));
    }

    /**
     * 提交修改
     */
    @Override
    public HomeStayResult editCapitalItem(CapitalItem capitalItem) {
        CapitalItemExample example = new CapitalItemExample();
        CapitalItemExample.Criteria criteria = example.createCriteria();
        criteria.andItemIdEqualTo(capitalItem.getItemId());
        capitalItemMapper.updateByExampleSelective(capitalItem, example);

        return HomeStayResult.ok();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public HomeStayResult delCapitalItem(Integer id) {
        CapitalItemExample example = new CapitalItemExample();
        CapitalItemExample.Criteria criteria = example.createCriteria();
        criteria.andItemIdEqualTo(id);
        capitalItemMapper.deleteByExample(example);
        return HomeStayResult.ok();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public HomeStayResult batchDelCapitalItem(List<Integer> ids) {
        CapitalItemExample example = new CapitalItemExample();
        CapitalItemExample.Criteria criteria = example.createCriteria();
        criteria.andItemIdIn(ids);
        capitalItemMapper.deleteByExample(example);
        return HomeStayResult.ok();
    }


}
